package dohmh.yelp.utilities;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import dohmh.yelp.exceptions.DOHMHException;

public class DataClean {
	public static void addCDATA(String source) throws DOHMHException{
		System.out.print("Cleaning feed file... ");
		BufferedReader in = null;
		BufferedWriter out = null;
		String dest = source.substring(0, source.lastIndexOf("\\")) + "\\_temp_" + source.substring(source.lastIndexOf("\\") + 1);
		try {
			in = new BufferedReader(new InputStreamReader(new FileInputStream(source), "UTF8"));
			FileOutputStream fos = new FileOutputStream(dest);
			out = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));
			String line;
			StringBuffer sb = new StringBuffer();
			while ((line = in.readLine()) != null){
				sb.append(new String(line.getBytes(), "UTF8"));
				if (sb.indexOf("<text>") > -1 && sb.indexOf("<text><![CDATA[") == -1){
					sb.insert(sb.indexOf("<text>") + 6, "<![CDATA[");
				}
				if (sb.indexOf("</text>") > -1 && sb.indexOf("]]></text>") == -1){
					sb.insert(sb.indexOf("</text>"), "]]>");
				}
				out.write(sb.toString());
				out.newLine();
				sb.delete(0, sb.length());
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			if (in != null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				finally {
					if (out != null){
						try {
							out.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		File s = new File(source);
		s.delete();
		File d = new File(dest);
		if (!d.renameTo(s)){
			throw new DOHMHException("Error cleaning data. Could not rename temp file.");
		}
		System.out.println("Done.");
	}
	public static void main(String[] args) throws DOHMHException{
		long start = System.currentTimeMillis();
		addCDATA(args[0]);
		long end = System.currentTimeMillis();
		System.out.println("Execution time was "+(end-start)+" ms.");
	}
}
